const util = require("../../utils/util.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodal: true,
    proType: "", //默认规格
    num: 0,      //默认数量
    currentPro: '',  //默认选中产品
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
      wx.request({
      url: app.globalData.url + 'productDetail',
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        code: options.code,
        proId: options.proId
      },
      success: function (res) {
        const pages = getCurrentPages();
        console.log(res.data.data);
       var content=res.data.data.replace(/src=\"/g, 'src="https://reagent.novogene.com');
       console.log(content);
        _this.setData({
          currentPro: pages[pages.length - 2].data.datalist[options.index],
          productDetail: content
        })
      },
      fail:function(res){
        wx.navigateBack();
      }

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  oepnType: function (e) {
   this.setData({
      hiddenmodal: false
    });
  },
  getType: function (e) {
    this.setData({
      proType: e.currentTarget.dataset.ml
    });
  },
  cancel: function (e) {
    this.setData({
      hiddenmodal: true,
      proType: "",
      num: 0
    });
  },
  confirm: function (e) {
    var key = this.data.currentPro.code + "||" + this.data.proType;
    var index = app.globalData.cartFlag.indexOf(key);
    if (this.data.num > 0) {
      if (this.data.currentPro.proId != 3 && this.data.proType == "") {
        wx.showToast({
          title: "请选择规格",
          icon: 'none',
          duration: 2000
        });
        return;
      }
      if (index == -1) {
        app.globalData.cartFlag.push(key);
        var tempobj = {
          "name": this.data.currentPro.name,
          "proType": this.data.proType,
          "num": this.data.num,
          "proId": this.data.currentPro.proId
        };
        app.globalData.carts.push(tempobj);
      } else {
        app.globalData.carts[index].num = this.data.num;
      }
    } else {
      if (index != -1) {
        app.globalData.carts.split(index, 1);
        app.globalData.cartFlag.split(index, 1);
      }
    }
    this.setData({
      hiddenmodal: true,
      proType: "",
      num: 0
    });
  },
  reduceNum: function () {
    if (this.data.num > 0) {
      this.setData({
        "num": this.data.num - 1
      })
    }
  },
  addNum: function () {
    this.setData({
      "num": this.data.num + 1
    })
  },
  gotoOrder: function () {
    var _this=this;
    wx.navigateTo({
      url: '../order/order?name=' + _this.data.currentPro.name + '&code=' + _this.data.currentPro.code + '&proId=' + _this.data.currentPro.proId+'&flag=1'
    })
  },
  testtap:function(e){
    return true;
  }
})